package lv.solcraft.replication.server.rest.restlets;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.restlet.Restlet;
import org.restlet.data.Request;
import org.restlet.data.Response;

public class ClientLogRestlet extends Restlet {

    private static final Logger log = Logger.getLogger(ClientLogRestlet.class);

    private final String serverName;

    public ClientLogRestlet(String serverName) {
        this.serverName = serverName;
    }

    @Override
    public void handle(Request request, Response response) {
        try {
            String site = request.getAttributes().get("site").toString();
            String eventType = request.getAttributes().get("event").toString();
            String logText = request.getEntity().getText();
            Logger log = Logger.getLogger("lv.solcraft.replication.server." + serverName + ".clientlog." + site);
            if ("DEBUG".equals(eventType))
                log.debug(logText);
            if ("INFO".equals(eventType))
                log.info(logText);
            if ("WARN".equals(eventType))
                log.warn(logText);
            if ("ERROR".equals(eventType))
                log.error(logText);
        } catch (IOException e) {
            log.warn("Client logging failed, " + e.getMessage());
        }
    }
}
